package com.honglu.user.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import com.honglu.utils.PropertiesUtil;

/**
 * redis配置
 * @author tb
 *
 */
@Configuration
public class RedisConfig {


    @Bean
    public JedisPoolConfig getRedisConfig(){
        JedisPoolConfig config = new JedisPoolConfig();
        return config;
    }

    @Bean
    public JedisPool getJedisPool() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(Integer.valueOf(PropertiesUtil.get("spring.redis.maxIdle")));
        poolConfig.setMaxTotal(Integer.valueOf(PropertiesUtil.get("spring.redis.maxTotal")));
        poolConfig.setTimeBetweenEvictionRunsMillis(-1);
        poolConfig.setTestOnBorrow(true);
        String host = PropertiesUtil.get("spring.redis.host");
        int port = Integer.valueOf(PropertiesUtil.get("spring.redis.port"));
        int timeout = Integer.valueOf(PropertiesUtil.get("spring.redis.timeout"));
        String pwd = PropertiesUtil.get("spring.redis.password");
        int db = Integer.valueOf(PropertiesUtil.get("spring.redis.db"));
        JedisPool pool = null;
        if (pwd == null || pwd.isEmpty()) {
            pool = new JedisPool(poolConfig, host, port, timeout);
        } else {
            pool = new JedisPool(poolConfig, host, port, timeout, pwd, db);
        }
        return pool;
    }
}
